package cn.Alibaba.system.service.impl;

import cn.Alibaba.system.domain.Role;
import cn.Alibaba.system.mapper.RoleMapper;
import cn.Alibaba.system.service.IRoleService;
import cn.Alibaba.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {


    @Autowired
    private RoleMapper roleMapper;


    @Override
    @Transactional
    public void update(Role role) {

        roleMapper.delRoleMenuByRoleId(role.getId());
        roleMapper.delRolePermissionByRoleIdRoleId(role.getId());

        addData(role);
        super.update(role);
    }

    @Override
    @Transactional
    public void save(Role role) {
        super.save(role);
        addData(role);
    }

    private void addData(Role role){
        List<Long> menus = role.getMenus();
        if (menus.size()>0){
            roleMapper.addRoleMenu(role.getId(),menus);
        }


        List<Long> permissions = role.getPermissions();
        if (permissions.size()>0){
            roleMapper.addRolePermissions(role.getId(),permissions);
        }
    }

    @Override
    @Transactional
    public void del(Long id) {

        roleMapper.delRoleMenuByRoleId(id);
        roleMapper.delRolePermissionByRoleIdRoleId(id);
        roleMapper.delEmployeeRoleByRoleId(id);

        super.del(id);
    }


}
